package top.wonew.helper.kugou.service;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;

public class KugouLoginSignPkService {
    private static final char[] f261821d = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static final String RSAPublicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIAG7QOELSYoIJvTFJhMpe1s/gbjDJX51HBNnEl5HXqTW6lQ7LC8jr9fWZTwusknp+sVGzwd40MwP6U5yDE27M/X1+UR4tvOGOqp94TJtQ1EPnWGWXngpeIW5GxoQGao1rmYWAu6oi1z9XkChrsUdC6DJE5E221wf/4WLFxwAtRQIDAQAB";

    public static String rsaEncrypt(String str) throws Exception {
        byte[] bArr = new byte[128];
        System.arraycopy(str.getBytes(), 0, bArr, 0, str.getBytes().length);
        RSAPublicKey rSAPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(m182143b(RSAPublicKeyStr)));
        Cipher cipher = Cipher.getInstance("RSA/ECB/NOPADDING");
        cipher.init(1, rSAPublicKey);
        byte[] res = cipher.doFinal(bArr);
        return m180454a(res).replace(" ", "").trim();
    }

    public static String m180454a(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(bArr.length * 2);
        for (byte b : bArr) {
            sb.append(f261821d[(b & 240) >>> 4]);
            sb.append(f261821d[b & 15]);
        }
        return sb.toString();
    }

    public static byte[] m182143b(String str) {
        int i;
        int i2 = 0;
        for (int length = str.length() - 1; str.charAt(length) == '='; length--) {
            i2++;
        }
        byte[] bArr = new byte[((str.length() * 6) / 8) - i2];
        int i3 = 0;
        for (int i4 = 0; i4 < str.length(); i4 += 4) {
            int m182148a = (m182148a(str.charAt(i4)) << 18) + (m182148a(str.charAt(i4 + 1)) << 12) + (m182148a(str.charAt(i4 + 2)) << 6) + m182148a(str.charAt(i4 + 3));
            for (int i5 = 0; i5 < 3 && (i = i3 + i5) < bArr.length; i5++) {
                bArr[i] = (byte) ((m182148a >> ((2 - i5) * 8)) & 255);
            }
            i3 += 3;
        }
        return bArr;
    }

    protected static int m182148a(char c) {
        if (c < 'A' || c > 'Z') {
            if (c < 'a' || c > 'z') {
                if (c < '0' || c > '9') {
                    if (c == '+') {
                        return 62;
                    }
                    if (c == '/') {
                        return 63;
                    }
                    return c == '=' ? 0 : -1;
                }
                return (c - '0') + 52;
            }
            return (c - 'a') + 26;
        }
        return c - 'A';
    }
}
